function calculate(str) {
  const regex = /(\d+)\s*([\+\-\*\/])\s*(\d+)/;
  const match = str.match(regex);
  if (!match) {
    throw new Error('Invalid input');
  }
  const [_, num1, operator, num2] = match;
  const n1 = parseInt(num1, 10);
  const n2 = parseInt(num2, 10);
  switch (operator) {
    case '+':
      return n1 + n2;
    case '-':
      return n1 - n2;
    case '*':
      return n1 * n2;
    case '/':
      if (n2 === 0) {
        throw new Error('Cannot divide by zero');
      }
      return n1 / n2;
    default:
      throw new Error('Invalid operator');
  }
}

console.log(calculate('1 + 2')); // 3
console.log(calculate('3 - 2')); // 1
console.log(calculate('2 * 3')); // 6
console.log(calculate('4 / 2')); // 2
console.log(calculate('1 / 0')); // Error: Cannot divide by zero
console.log(calculate('1 & 2')); // Error: Invalid operator

const http = require('http');
const fs = require('fs');

const url = 'http://ts1.cn.mm.bing.net/th/id/R-C.987f582c510be58755c4933cda68d525?rik=C0D21hJDYvXosw&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc%2ftx%2fwallpaper%2f1305%2f16%2fc4%2f20990657_1368686545122.jpg&ehk=netN2qzcCVS4ALUQfDOwxAwFcy41oxC%2b0xTFvOYy5ds%3d&risl=&pid=ImgRaw&r=0';
const dest = './image.png';

const file = fs.createWriteStream(dest);

http.get(url, (response) => {
  response.pipe(file);
});

const arr = Array.from({ length: 100 }, () => Math.floor(Math.random() * 100));
const fs =require('fs');
for (let i = 1; i < arr.length; i++) {
  const temp = arr[i];
  let j = i - 1;
  while (j >= 0 && arr[j] > temp) {
    arr[j + 1] = arr[j];
    j--;
  }
  arr[j + 1] = temp;
}

fs.writeFileSync('./sorted.txt', arr.join(','));